; *******************************************************************
;   gobj_control.ahk
; *******************************************************************

; -------------------------------------------------------------------
;   includes
; -------------------------------------------------------------------

; #include filex.ahk

; -------------------------------------------------------------------
;   gobj_control
; -------------------------------------------------------------------

class gobj_control
{
  _priv_ := {  id:      "undefined" 
             , form:    ""
             , options: ""
             , title:   ""}
 
  __New(opt="", title="")
  {
    this._priv_.options := opt
    this._priv_.title   := title
    this._priv_.precreate  := {}
    this._priv_.precreate.properties  := []
  }

  ; -----------------------------------------------------------------
  ;   OnInsertControl
  ; -----------------------------------------------------------------

  OnInsertControl(form, idname, type="button")
  {
    global
    local varname,GuiName,opt,title,k,v,precreate
    this._priv_.form := form
    varname          := form.getID() . "_" . idname
    this._priv_.id   := varname 
    GuiName          := form.getID()
    opt              := this._priv_.options
    title            := this._priv_.title

    if type in progress,text 
      Gui %GuiName%:Add, %type%, %opt% v%varname%, %title%
    else
      Gui %GuiName%:Add, %type%, %opt% v%varname% ggobj_command, %title%
      
    for k, v in this._priv_.precreate.properties
    precreate := this._priv_.precreate
    this._priv_.Remove("precreate")
    for k, v in precreate.properties
    {
      this[v.name] := v.value    
    }
  }

  ; -----------------------------------------------------------------
  ;   OnSize
  ; -----------------------------------------------------------------

  OnSize(w, h)
  {
    if (this._priv_.HasKey("anchor_pos"))
      stdlib_anchor(this._priv_.id, this._priv_.anchor_pos, 0)
  }

  ; -----------------------------------------------------------------
  ;   __Set
  ; -----------------------------------------------------------------

  __Set(aName, aValue)
  {
    if aName in x,y,w,h,text
    {
      if (this._priv_.HasKey("precreate"))
      {
        p := {}
        p.name  := aName
        p.value := aValue
        this._priv_.precreate.properties.Insert(p)
      }
      else
      {
        formid := this._priv_.form.getID()
        cntlid := this._priv_.id
        GuiControlGet rect, %formid%:Pos, %cntlid%
        if (aName = "x")
          rectX := aValue
        if (aName = "y")
          rectY := aValue
        if (aName = "w")
          rectW := aValue
        if (aName = "h")
          rectH := aValue
        if (aName = "text")
          GuiControl %formid%: , %cntlid%, %aValue%
        else
          GuiControl %formid%:Move, %cntlid%, x%rectX% y%rectY% w%rectW% h%rectH%    
      }
      return aValue
    }

    if aName in anchor
    {
      this._priv_.anchor_pos := aValue
      return aValue
    }
  }  

  ; -----------------------------------------------------------------
  ;   __Get
  ; -----------------------------------------------------------------

  __Get(aName)
  {
    if aName in x,y,w,h,text
    {
      formid := this._priv_.form.getID()
      cntlid := this._priv_.id
      GuiControlGet rect, %formid%:Pos, %cntlid%
      if (aName = "x")
        return rectX
      if (aName = "y")
        return rectY
      if (aName = "w")
        return rectW
      if (aName = "h")
        return rectH
      if (aName = "text")
      {
        GuiControlGet v, %formid%: , %cntlid%
        return v
      }
    }

    if aName in anchor
    {
      return this._priv_.anchor_pos
    }
  }
}

; -------------------------------------------------------------------
;   gobj_button
; -------------------------------------------------------------------

class gobj_button extends gobj_control
{
  __New(opt="", title="")
  {
    base.__New(opt, title)
  }

  OnInsertControl(form, idname)
  {
    this.OnCommand := form["On" . idname]
    base.OnInsertControl(form, idname, "button")
  }
}

; -------------------------------------------------------------------
;   gobj_checkbox
; -------------------------------------------------------------------

class gobj_checkbox extends gobj_control
{
  __New(opt="", title="")
  {
    base.__New(opt, title)
  }

  OnInsertControl(form, idname)
  {
    base.OnInsertControl(form, idname, "checkbox")
  }
}

; -------------------------------------------------------------------
;   gobj_text
; -------------------------------------------------------------------

class gobj_text extends gobj_control
{
  __New(opt="", title="")
  {
    base.__New(opt, title)
  }

  OnInsertControl(form, idname)
  {
    base.OnInsertControl(form, idname, "text")
  }
}

; -------------------------------------------------------------------
;   gobj_edit
; -------------------------------------------------------------------

class gobj_edit extends gobj_control
{
  __New(opt="", title="")
  {
    base.__New(opt, title)
  }

  OnInsertControl(form, idname)
  {
    base.OnInsertControl(form, idname, "edit")
  }
}

; -------------------------------------------------------------------
;   gobj_listbox
; -------------------------------------------------------------------

class gobj_listbox extends gobj_control
{
  __New(opt="", title="")
  {
    base.__New(opt, title)
  }

  OnInsertControl(form, idname)
  {
    base.OnInsertControl(form, idname, "listbox")
  }

  Choose(idx=1)
  {
    id := this._priv_.id
    GuiControl, Choose, %id%, %idx%
  }
}

; -------------------------------------------------------------------
;   gobj_progress
; -------------------------------------------------------------------

class gobj_progress extends gobj_control
{
  __New(opt="", title="")
  {
    base.__New(opt, title)
  }
  
  OnInsertControl(form, idname)
  {
    base.OnInsertControl(form, idname, "progress")
  }

  ; -----------------------------------------------------------------
  ;   __Set
  ; -----------------------------------------------------------------

  __Set(aName, aValue)
  {
    if (aName = "pos") 
    {
      formid := this._priv_.form.getID()
      cntlid := this._priv_.id
      GuiControl %formid%: , %cntlid%, %aValue%
      return aValue
    }
  }  
}

